package org.javaboy.redis.web;

import com.sanbi.common.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.javaboy.redis.common.RedisService;
import org.javaboy.redis.service.HelloService;
import org.javaboy.redis.model.User;
import org.javaboy.redis.service.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
public class UserController {
    @Autowired
    private HelloService helloService;
    @Autowired
    private OrderNoUtil orderNoUtil;

    @GetMapping("getUserRed")
    @Cacheable(value="user:red",key = "#id")
    public User getUserRed(Long id) {
//        Long id= StringUtil.getRandomNum(5);
        User user=new User(id,"aa@126.com", "aa", "aa123456", "aa","123");
        System.out.println("若下面没出现“无缓存的时候调用”字样且能打印出数据表示测试成功");
        return user;
    }

    @GetMapping("getUserGreen")
    @Cacheable(value="user:green",key = "#id")
    public User getUserGreen(Long id) {
//        Long id= StringUtil.getRandomNum(5);
        User user=new User(id,"aa@126.com", "aa", "aa123456", "aa","123");
        System.out.println("若下面没出现“无缓存的时候调用”字样且能打印出数据表示测试成功");
        return user;
    }

    @PostMapping("updateUser")
    @CachePut(value="user",key = "#id")
    public User updateUser(Long id) {
        String name= StringUtil.getRandomString(6);
        User user=new User(id,"aa@126.com", name, "aa123456", "aa","123");
        System.out.println("若下面没出现“无缓存的时候调用”字样且能打印出数据表示测试成功");
        return user;
    }

    @PostMapping("deleteUser")
    @CacheEvict(value="user",key = "#id")
    public String deleteUser(Long id) {
        String msg=String.format("用户已删除，id：%s",id);
        return msg;
    }

    @GetMapping("/uid")
    public String uid(HttpSession session) {
        UUID uid = (UUID) session.getAttribute("uid");
        if (uid == null) {
            uid = UUID.randomUUID();
        }
        session.setAttribute("uid", uid);
        return session.getId();
    }

    @GetMapping("hello")
    public String hello(String name){
        return helloService.hello(name);
    }

    @GetMapping("hello2")
    public String hello2(String name){
        return helloService.hello2(name);
    }

    @GetMapping("setGroup")
    public String setGroup(){
        return helloService.setGroup();
    }

    @GetMapping("getList")
    public Map<String, List<String>> getList(){
        return helloService.getList();
    }

    @GetMapping("testRedis")
    public String testRedis(){
        return helloService.redisTest();
    }

    @GetMapping("getOrderNo")
    public String getOrderNo(){
        String orderNo=orderNoUtil.generateOrderNo("PO");

        return orderNo;
    }
}